package com.iot.system.core.impl;


import com.iot.common.utils.DateUtils;
import com.iot.system.core.CoreLineHourEnergyInfoService;
import com.iot.system.domain.LineHourEnergyInfo;
import com.iot.system.domain.LineHourYieldsInfo;
import com.iot.system.domain.ProductionLineDeviceRelation;
import com.iot.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CoreLineHourEnergyInfoServiceImpl implements CoreLineHourEnergyInfoService {



    @Resource
    private IProductionLineDeviceRelationService productionLineDeviceRelationService;
    @Resource
    private IDeviceEnergyService iDeviceEnergyService;
    @Resource
    private ILineHourEnergyInfoService iLineHourEnergyInfoService;
    @Resource
    private ILineHourYieldsInfoService iLineHourYieldsInfoService;
    @Resource
    private IDeviceYieldsService iDeviceYieldsService;


    @Override
    public void dealWithLineEnergy(long lineId, Date date) {

        log.info("CoreLineHourEnergyInfoServiceImpl_dealWithLineEnergy_处理产线能耗统计开始_产线:{},时间:{}", lineId, date);
        Date lastHourEndByDate = DateUtils.getLastHourEndByDate(new Date());
        Date lastHourStartByDate = DateUtils.getLastHourStartByDate(new Date());
        if (date!=null){
            if (!(DateUtils.isDateBeforeDate(date, new Date()))) {
                log.info("CoreLineHourEnergyInfoServiceImpl_dealWithLineEnergy_处理产线能耗统计结束_时间小于当前时间_参数时间:{},当前时间:{}", date, new Date());
                return;
            }
            lastHourEndByDate = DateUtils.getLastHourEndByDate(date);
            lastHourStartByDate = DateUtils.getLastHourStartByDate(date);
        }

        ProductionLineDeviceRelation productionLineDeviceRelation = new ProductionLineDeviceRelation();
        productionLineDeviceRelation.setLineId(lineId);
        LineHourEnergyInfo lineHourEnergy = new LineHourEnergyInfo();
        lineHourEnergy.setLineId(lineId);
        lineHourEnergy.setLineTime(lastHourStartByDate);
        lineHourEnergy.setLineDate(lastHourEndByDate);

        //判断当前时间是否计算完成
        Boolean flag = iLineHourEnergyInfoService.judgmentLineHourEnergy(lineHourEnergy);
        log.info("CoreLineHourEnergyInfoServiceImpl_dealWithLineEnergy_判断当前时间是否已经完成能耗统计_flag:{}", flag);
        if (!flag){
            return;
        }
        //获取产线相关的设备ID
        List<ProductionLineDeviceRelation> productionLineDeviceRelations = productionLineDeviceRelationService.selectProductionLineDeviceRelationList(productionLineDeviceRelation);
        //判断是否存在设备
        if(CollectionUtils.isEmpty(productionLineDeviceRelations)){
            return;
        }
        //获取设备ID集合
        List<Long> collect = productionLineDeviceRelations.stream().map(ProductionLineDeviceRelation::getDeviceId).collect(Collectors.toList());
        //处理时间 当前的定时任务仅处理 上一个小时的能耗数据
        //根据当前时间获取上一个小时的开始时间和结束时间
        String energy = iDeviceEnergyService.getLineHourEnergy(collect,lastHourStartByDate,lastHourEndByDate);
        if (energy==null||energy.equals("")){
            return;
        }
        //更新产线能耗
        LineHourEnergyInfo lineHourEnergyInfo = new LineHourEnergyInfo();
        lineHourEnergyInfo.setEnergyAccumulate(energy);
        lineHourEnergyInfo.setLineTime(lastHourStartByDate);
        lineHourEnergyInfo.setLineDate(lastHourEndByDate);
        lineHourEnergyInfo.setLineId(lineId);
        iLineHourEnergyInfoService.insertLineHourEnergyInfo(lineHourEnergyInfo);
        log.info("CoreLineHourEnergyInfoServiceImpl_dealWithLineEnergy_处理产线能耗统计结束_插入信息:{}", lineHourEnergyInfo);
    }

    @Override
    public void countLineHourYieldsInfo(long lineId,Date date) {
        log.info("CoreLineHourEnergyInfoServiceImpl_countLineHourYieldsInfo_统计每小时产线产能开始_产线:{}", lineId,date);
        Date lastHourEndByDate = DateUtils.getLastHourEndByDate(new Date());
        Date lastHourStartByDate = DateUtils.getLastHourStartByDate(new Date());
        if (date!=null){
            if (!(DateUtils.isDateBeforeDate(date, new Date()))) {
                log.info("CoreLineHourEnergyInfoServiceImpl_dealWithLineEnergy_处理产线能耗统计结束_时间小于当前时间_参数时间:{},当前时间:{}", date, new Date());
                return;
            }
            lastHourEndByDate = DateUtils.getLastHourEndByDate(date);
            lastHourStartByDate = DateUtils.getLastHourStartByDate(date);
        }
        LineHourYieldsInfo lineHourYieldsInfo = new LineHourYieldsInfo();
        lineHourYieldsInfo.setLineId(lineId);
        lineHourYieldsInfo.setLineStartTime(lastHourStartByDate);
        lineHourYieldsInfo.setLineDate(lastHourEndByDate);
        Boolean flag = iLineHourYieldsInfoService.judgmentLineHourEnergy(lineHourYieldsInfo);
        log.info("CoreLineHourEnergyInfoServiceImpl_countLineHourYieldsInfo_判断当前时间是否已经完成产量统计_flag:{}", flag);
        if (!flag){
            return;
        }
        ProductionLineDeviceRelation productionLineDeviceRelation = new ProductionLineDeviceRelation();
        productionLineDeviceRelation.setLineId(lineId);
        //获取产线相关的设备ID
        List<ProductionLineDeviceRelation> productionLineDeviceRelations = productionLineDeviceRelationService.selectProductionLineDeviceRelationList(productionLineDeviceRelation);
        //判断是否存在设备
        if(CollectionUtils.isEmpty(productionLineDeviceRelations)){
            return;
        }
        //获取设备ID集合  这块是个开关  目前只统计检测机的产量
        List<Long> collect = productionLineDeviceRelations.stream().map(ProductionLineDeviceRelation::getDeviceId).filter(item-> Objects.equals(6L,item)).collect(Collectors.toList());
        log.info("CoreLineHourEnergyInfoServiceImpl_countLineHourYieldsInfo_获取产线设备ID_设备ID:{}", collect);
        if (CollectionUtils.isEmpty(collect)){
            return;
        }
        Integer yields =iDeviceYieldsService.getLineHourYields(collect,lastHourStartByDate,lastHourEndByDate);
        if (yields==null||yields.equals("")){
            return;
        }
        lineHourYieldsInfo.setYieldsAccumulate(yields.toString());
        lineHourYieldsInfo.setLineEndTime(lastHourEndByDate);
        iLineHourYieldsInfoService.insertLineHourYieldsInfo(lineHourYieldsInfo);
    }
}
